<?php

declare(strict_types=1);

namespace App\Repositories\Gashapon;

use App\Models\MainDB\Gashapon\Draw as Model;
use App\Repositories\GiftRepository;
use App\Services\Tools\PushChatService;
use App\Services\Tools\PushService;
use App\Services\Tools\RedisService;
use App\Traits\Singleton;

class PushMessageRepository
{
    use Singleton;

    public function pushGashaponMessage($userId, $giftIdList, $type)
    {
        try {
            $giftIds        = array_column($giftIdList, 'gift_id');
            $giftListWithId = array_column($giftIdList, null, 'gift_id');
            $giftInfoList   = app('repo_gift')->giftsByIds($giftIds);
            $giftInfoList->transform(function ($item, $key) use ($giftListWithId) {
                $item->count = $giftListWithId[$item->id]['count'];
                return $item;
            });

            $user = RedisService::getUserChatLabel($userId);
            if ($type == 0 || $type == 1) {
                foreach ($giftInfoList as $giftData) {
                    (new PushChatService())->gashapon($user, $giftData);
                }
            }
            if ($type == 0 || $type == 2) {
                (new PushService())->gashapon($giftInfoList, $user);
            }
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }
}
